package cn.train.controler;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.train.model.Admin;
import cn.train.service.BackLoginServices;

@WebServlet("/BackLoginServlet")
public class BackLoginServlet extends HttpServlet {
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String name=req.getParameter("name");
		String pwd=req.getParameter("pwd");
		if(name!=null&&pwd!=null){
		BackLoginServices backl=new BackLoginServices();
		System.out.println("正在检查");
		Admin ad=backl.findAll(name, pwd);
		PrintWriter out=resp.getWriter();
		HttpSession session =req.getSession();
		System.out.println("正在检查..."+ad);
		if(ad!=null){
		
			if(ad.getAstate()==0&&ad!=null){
				System.out.println("正在存session..."+ad.getAstate());
				session.setAttribute("adminuser", ad);
				resp.sendRedirect("/back/admin/index.jsp");
			}else if(ad.getAstate()!=0&&ad!=null){
				
				resp.sendRedirect("/back/login.jsp?flag=1");
			}
		}else{
			resp.sendRedirect("/back/login.jsp?flag=2");
		}
     }
	}
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
}
